package com.smssalama.gui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;

import com.smssalama.AlertManager;
import com.smssalama.SMSSalamaSession;
import com.smssalama.i18n.CommandLabels;
import com.smssalama.i18n.Labels;
import com.smssalama.i18n.ScreenTitles;
import com.smssalama.storage.contact.Contact;

/**
 * Provides a read only view of a contact's details
 * 
 * @author Arnold P. Minde
 *
 */
public class ContactViewer extends Form implements CommandListener {
	private Labels LABELS = Labels.labels();
	private CommandLabels CMD_LABELS = CommandLabels.cmdLabels();

	Displayable back;
	Contact contact;

	StringItem phoneNumberField = new StringItem(LABELS.phoneNumber()
			+ "\n", "", Item.PLAIN);
	StringItem contactNameField = new StringItem(LABELS.contactName()
			+ "\n", "", Item.PLAIN);
	StringItem passwordField = new StringItem(LABELS.password() + "\n",
			"", Item.PLAIN);
	StringItem emailField = new StringItem(LABELS.email() + "\n", "",
			Item.PLAIN);
	StringItem noteField = new StringItem(LABELS.note() + "\n", "",
			Item.PLAIN);

	Command backCmd = new Command(CMD_LABELS.back(), Command.BACK,
			1);
	Command deleteCmd = new Command(CMD_LABELS.delete(),
			Command.OK, 2);
	Command createSmsCmd = new Command(LABELS.createSMS(), Command.OK, 3);

	/**
	 * Initializes a contacts viwer form.
	 * 
	 * @param contact - The contact to be displayed
	 * @param back - The {@link Displayable} that should be shown when the <code>back</code> command is invoked.
	 */
	public ContactViewer(Contact contact, Displayable back) {
		super(ScreenTitles.screenTitles().contactView());
		this.back = back;
		this.contact = contact;
		init(contact);
	}

	/**
	 * Initializes a contacts viwer form.
	 * 
	 * @param contact - The contact to be displayed
	 */
	public ContactViewer(Contact contact) {
		super(ScreenTitles.screenTitles().contactView());
		this.back = null;
		this.contact = contact;
		init(contact);
	}
	
	/**
	 * Initializes a contacts viwer form.
	 * 
	 * @param contactId - The id of the contact to be shown. 
	 * @param back - The {@link Displayable} that should be shown when the <code>back</code> command is invoked.
	 * @throws Exception if the contact with the specified id can not be retrieved
	 */
	public ContactViewer(int contactId, Displayable back) throws Exception {
		this((Contact)SMSSalamaSession.getSession().getUserSettings().getContactsStore().getStorable(contactId), back);
	}

	public ContactViewer(int contactId) throws Exception {
		this((Contact)SMSSalamaSession.getSession().getUserSettings().getContactsStore().getStorable(contactId));
	}

	public void init(Contact contact) {
		this.setCommandListener(this);

		this.phoneNumberField.setText(contact.getPhoneNumber());
		this.phoneNumberField.setPreferredSize(Integer.MAX_VALUE, -1);
		this.contactNameField.setText(contact.getContactName());
		this.contactNameField.setPreferredSize(Integer.MAX_VALUE, -1);
		this.passwordField.setText(contact.getSharedKey());
		this.passwordField.setPreferredSize(Integer.MAX_VALUE, -1);
		this.emailField.setText(contact.getEmail());
		this.emailField.setPreferredSize(Integer.MAX_VALUE, -1);
		this.noteField.setText(contact.getNote());
		this.noteField.setPreferredSize(Integer.MAX_VALUE, -1);

		this.append(this.contactNameField);
		this.append(this.phoneNumberField);
		this.append(this.passwordField);
		this.append(this.emailField);
		this.append(this.noteField);
		
		if(back!=null){
			this.addCommand(this.backCmd);
		}
	}

	public void commandAction(Command command, Displayable displayable) {
		if (command.getCommandType() == Command.BACK) {
			if(this.back!=null){
				AlertManager.showDisplayable(this.back);
			}else{
				AlertManager.showDisplayable(Home.getHomeScreen()); 
			}
		} else if (command == this.deleteCmd) {
			try {
				SMSSalamaSession.getSession().getUserSettings()
						.getContactsStore().deleteStorable(
								this.contact.getRecordId());
				AlertManager.showDisplayable(this.back);
			} catch (Exception e) {
				e.printStackTrace();
				AlertManager.showError(this, e);
			}
		} else if (command == this.createSmsCmd) {
			// TODO: Send SMS from ContactViewer
		}
	}
}
